package com.sooth.system.tenant.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.dream.system.config.Page;
import com.sooth.common.core.domain.R;
import com.sooth.common.dream.model.PageModel;
import com.sooth.common.idempotent.annotation.RepeatSubmit;
import com.sooth.common.log.annotation.Log;
import com.sooth.common.satoken.utils.LoginHelper;
import com.sooth.common.web.core.BaseController;
import com.sooth.system.tenant.service.ISysTenantService;
import com.sooth.system.tenant.view.SysTenantBo;
import com.sooth.system.tenant.view.SysTenantDto;
import com.sooth.system.tenant.view.SysTenantVo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 租户管理
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/tenant")
public class SysTenantController extends BaseController {

    private final ISysTenantService tenantService;

    /**
     * 查询租户列表
     */
    @SaCheckPermission("system:tenant:query")
    @GetMapping("/list")
    public R<Page<SysTenantVo>> list(SysTenantDto tenant, PageModel pageModel) {
        return R.ok(tenantService.selectPage(tenant, pageModel));
    }

    /**
     * 获取租户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:tenant:query")
    @GetMapping("/query")
    public R<SysTenantBo> query(@RequestParam Long id) {
        return R.ok(tenantService.selectById(id));
    }

    /**
     * 新增租户
     */
    @SaCheckPermission("system:tenant:add")
    @Log(title = "租户管理", message = "新增租户")
    @PostMapping("/add")
    public R<Void> add(@RequestBody SysTenantBo tenantBo) {
        return toAjax(tenantService.insert(tenantBo));
    }

    /**
     * 修改租户
     */
    @SaCheckPermission("system:tenant:edit")
    @Log(title = "租户管理", message = "修改租户")
    @RepeatSubmit()
    @PutMapping("/edit")
    public R<Void> edit(@RequestBody SysTenantBo tenant) {
        return toAjax(tenantService.updateById(tenant));
    }


    /**
     * 删除租户
     *
     * @param id 主键
     */
    @SaCheckPermission("system:tenant:edit")
    @Log(title = "租户管理", message = "删除租户")
    @DeleteMapping("/remove")
    public R<Void> remove(@RequestParam Long id) {
        return toAjax(tenantService.deleteById(id));
    }

    /**
     * 切换多租户
     *
     * @param tenantId
     * @return
     */
    @SaCheckPermission("system:tenant:switch")
    @GetMapping("/switch")
    public R<Void> switchTenant(@RequestParam String tenantId) {
        LoginHelper.setTenantId(tenantId);
        return R.ok();
    }

}
